package com.soriya.switcher.util

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.Settings
import com.blankj.utilcode.util.PermissionUtils
import okhttp3.Callback

object PermissionUtils {

    fun checkStoragePermission(context: Context): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!Environment.isExternalStorageManager()) {
                val intent = Intent(
                    Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
                    Uri.fromParts("package", context.packageName, null)
                )
                context.startActivity(intent)
                return false
            }
        } else {
            if (!PermissionUtils.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                PermissionUtils
                    .permission(Manifest.permission.READ_EXTERNAL_STORAGE)
                    .request()
                return false
            }
        }
        return true
    }

    fun checkRecordPermission(): Boolean {
        if (!PermissionUtils.isGranted(Manifest.permission.RECORD_AUDIO)) {
            PermissionUtils.permission(Manifest.permission.RECORD_AUDIO).request()
            return false
        }
        return true
    }

    fun checkLocation(): Boolean {
        if (!PermissionUtils.isGranted(Manifest.permission.ACCESS_FINE_LOCATION)) {
            PermissionUtils.permission(Manifest.permission.ACCESS_FINE_LOCATION).request()
            return false
        }
        return true
    }

    fun checkCameraPermission(callback: PermissionUtils.SingleCallback? = null): Boolean {
        if (!PermissionUtils.isGranted(Manifest.permission.CAMERA)) {
            PermissionUtils
                .permission(Manifest.permission.CAMERA)
                .callback(callback)
                .request()
            return false
        }
        return true
    }

}